<template>
  <div class="container">
    <div class="top">
      <el-button type="primary" @click="handelClick(item)" v-for="(item ,i) in options" :key="i"> {{ item }}</el-button>

      <el-button type="primary" @click="add(0)">添加游戏</el-button>
      <el-button type="danger" @click="Delete">一键删除</el-button>
      <el-button type="success" @click="jifen">积分修改</el-button>
      <input type="text" placeholder="请输入修改后的积分" v-model="currentScore"
             style="margin-left: 10px;line-height: 25px;text-indent: 1em">
    </div>
    <div class="box">
      <el-table height="70vh" :data="goods[0]" style="width: 100%;" @row-click="current"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"/>

        <el-table-column sortable prop="id" label="id" width="150"/>
        <el-table-column prop="img"
                         label="图片" width="120">
          <template #default="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.img" :fit="fit"/>
          </template>
        </el-table-column>

        <el-table-column sortable prop="name" label="名称" width="150"/>
        <el-table-column sortable prop="score" label="积分" width="120"/>
        <el-table-column prop="download" label="下载量" width="120"/>
        <el-table-column prop="visit" label="浏览量" width="120"/>
        <el-table-column sortable prop="weight" label="权重" width="120"/>
        <el-table-column prop="account" label="账号" width="120"/>
        <el-table-column prop="pwd" label="密码" width="120"/>
        <el-table-column prop="language" label="语言" width="300"/>
        <el-table-column sortable prop="category" label="类别" width="300"/>
        <el-table-column label="简介" width="300">
          <template #default="scope">
            <div class="text">
              {{ scope.row.info }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="Operations" width="120" fixed="right">
          <template #default slot-scope="scope">
            <el-button link type="primary" size="small" @click="add(1)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :page-size="30"

          layout="prev, pager, next"
          :total="total"
          @current-change="handleCurrentChange"
      />
    </div>


    <el-dialog title="商品信息" v-model="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth">
          <file @getName="getName1"></file>
        </el-form-item>

        <el-form-item label="积分" :label-width="formLabelWidth">
          <el-input v-model="form.score" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="权重" :label-width="formLabelWidth">
          <el-input v-model="form.weight" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="账号" :label-width="formLabelWidth">
          <el-input v-model="form.account" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="form.pwd" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" :label-width="formLabelWidth">
          <el-input v-model="form.info" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="语言" :label-width="formLabelWidth">
          <el-input v-model="form.language" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类别" :label-width="formLabelWidth">
          <el-input v-model="form.category" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="finish">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import {ElMessage} from 'element-plus'

const currentScore = ref('')
const flag = ref(true)//false:添加
import file from '../components/upload'
import {reactive, ref, onMounted, inject} from "vue";
import copy from "@/utils/copy";

const total = ref(0)
const options = reactive([
  "近期热度",
  "最多人玩",
  "推荐游戏"
])
const deleteList = []
const type = ref("")
const form = reactive({
  name: "",
  score: 0,
  visit: 0,
  weight: 1,
  download: 0,
  img: "",
  account: "",
  pwd: "",
  info: "",
  id: 0,
  category: '',
  language: ''

})
const formLabelWidth = ref(100)
const axios = inject("$axios")
const dialogFormVisible = ref(false)
const close = () => dialogFormVisible.value = false
const goods = reactive([
  []])
onMounted(() => {
  axios.get("goods/manage/list?kind=近期热度&p=0&s=30").then(res => {
    goods[0] = res.data.data.data
    total.value = res.data.data.p


  }).catch(err => {
    ElMessage.error('网络异常')
  })
})
const current = (row, a, b) => {
  for (var key in form) {
    form[key] = row[key]
  }
  console.log(row, a, b)

}
const handelClick = (i) => {
  type.value = i
  axios.get("goods/manage/list?p=0&s=30&kind=" + i).then(res => {
    goods[0] = res.data.data.data

    console.log(goods)
  }).catch(err => {
    console.log(err)
    ElMessage.error('网络异常')
  })
}

const add = (e) => {
  if (e == 0) {
    for (var key in form) {
      form[key] = ''
    }
    flag.value = false
  }
  dialogFormVisible.value = true
}
const handleCurrentChange = val => {
  page.value = val
  axios.get("goods/manage/list?p=" + val + "&s=30&kind=" + type.value).then(res => {
    console.log(res)
    goods[0] = res.data.data.data
    total.value = res.data.data.p
  }).catch(err => {

  })
}

const finish = () => {
  var flag1 = true
  for (var key in form) {
    if (form[key] == '' && key != 'visit' && key != 'download' && key != 'id' && key != 'language' && key != 'category') flag1 = false
  }
  console.log(form)
  if (flag1) {
    if (!flag.value) {
      axios.post("goods", form).then(res => {
        ElMessage.success(res.data.data)

      }).catch(res => {
      })
      flag.value = true
    } else {
      axios.post("goods/update", form).then(res => {
        ElMessage.success(res.data.data)

      }).catch(res => {
      })
    }

    window.location.reload()
  } else ElMessage.error('请完整填写信息')
}
const baseUrl = inject("$baseURL")
const getName1 = (value) => {
  form.img = baseUrl + value.value
}

const Delete = () => {
  setTimeout(() => {
    if (deleteList.length == 0) {
      ElMessage.error('请选择')
    } else
      axios.post("goods/delete", deleteList).then(res => {
        ElMessage.success(res.data.data)
        window.location.reload()
      }).catch(res => {
      })
    window.location.reload()

  }, 500)
}
const handleSelectionChange = val => {
  deleteList.push(val[val.length - 1].id)
  console.log(deleteList)
}
const jifen = () => {
  const exp = /^[0-9]+.?[0-9]*/;

  if (deleteList.length == 0) {
    ElMessage.error('请选择')
  } else if (!exp.test(currentScore.value)) {
    ElMessage.error('请输入合法的积分')
  } else
    axios.post("goods/updateScore?score=" + currentScore.value, deleteList).then(res => {
      ElMessage.success(res.data.data)
     window.location.reload()
    }).catch(res => {
    })


}


</script>

<style scoped lang="less">


.box {
  width: 90%;
  max-width: 1279px;
  background-color: #ffffff;
  margin: 10px auto;
  height: 80vh;
  max-height: 800px;
  overflow: hidden;

}

.text {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
}

.top {
  width: 90%;
  box-sizing: border-box;
  display: flex;
  justify-content: flex-start;
  padding: 10px 30px;
  align-items: center;
  margin: 20px auto;
  background-color: rgba(255, 255, 255, .9);
  height: 40px;
  border-radius: 20px;
  line-height: 40px;
  box-shadow: 1px 1px 10px #bacfe7;

}

</style>